<template>
	<view class="content">
		<web-view :src="viewUrl" @onPostMessage="handlePostMessage" @message="receiveMessage"></web-view>
	</view>
</template>

<script>
	export default {
		name: "",
		data() {
			return {
				modelUrl: "",
				viewUrl: "",
			};
		},
		onLoad: function(option) {
			this.modelUrl = option.modelUrl;
			this.viewUrl = "http://192.168.1.6:8080/view?modelUrl=" + this.modelUrl

			//添加web页面消息监听器
			this.webLoadHandle();
		},
		create() {

		},
		methods: {
			// 实时消息接收只支持APP-nvue
			handlePostMessage(e) {
				let res = e.detail.data;
				console.log("1接收到值：" + JSON.stringify(res));
				// uni.switchTab({
				// 	url: '/pages/cart/cart'
				// })
			},
			// 特定时机（后退、组件销毁、分享）触发并收到消息
			receiveMessage(e) {
				let res = e.detail.data;
				console.log("2接收到值：" + JSON.stringify(res));
				// uni.switchTab({
				// 	url: '/pages/home/home'
				// })
			},
			// H5向H5发送消息是通过window.postMessage
			webLoadHandle() {
				// #ifdef H5
				window.addEventListener("message", function(e) {
					if (e.data.type === "message") {
						console.log("3接收到值：" + JSON.stringify(e.data.data));
					} else if (e.data.type === "jump") {
						uni.switchTab({
							url: '/pages/home/home'
						})
					}
				});
				// #endif
			},
		},
	}
</script>

<style scoped>
	.content {}
</style>